<?php
/*
	收入明细
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']==6){
		$params['status']=0;
		$tstart= mktime(0,0,0,date('m'),date('d'),date('Y'));
		$tend =$tstart+24*3600;
		$params['timenum between']=array($tstart,$tend);
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($_RQ['memberid']){
		$params['memberid']=intval($_RQ['memberid']);
		$return['params']['memberid']=$_RQ['memberid'];
	}
	if($_RQ['title']){
		$params2['title like']='%'.trim($_RQ['title']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['mobile']){
		$params2['mobile like']='%'.trim($_RQ['mobile']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['mobile']=$_RQ['mobile'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['timenum between']=array($time_range['tstart'],$time_range['tend']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$total='';
	$list=pdo_getslice('jy_yxw_fentan_log',$params,array($pindex,$psize),$total,$fields,'','id asc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('xuesheng')->member_show($item['xueshengid']);
			$xuesheng['status']=pdo_getcolumn('jy_yxw_class',array('id'=>$item['xueshengid'],'uniacid'=>$_SESSION['uniacid']),'status');
			$item['xsstatustitle']=$this->xuesheng_status[$xuesheng['status']];
			$infee_log=pdo_get('jy_yxw_infee_log',array('type'=>1,'memberid'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']));
			$item['paytime']=$infee_log['up_time'];
			$item['payfee']=$infee_log['fee'];
			$item['pay_type']=$infee_log['pay_type'];
			$item['classid']=pdo_getcolumn('jy_yxw_xuesheng',array('id'=>$item['xueshengid'],'uniacid'=>$_SESSION['uniacid']),'classid');
			$item['open_time']=pdo_getcolumn('jy_yxw_class',array('id'=>$item['classid'],'uniacid'=>$_SESSION['uniacid']),'open_time');
			$item['zhanghu']='公户';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：这里处理各门店的收费情况','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select2','options'=>'stores'),
			array('name'=>'classid','title'=>'售后群','type'=>'select2','options'=>'class'),
			array('type'=>'string','title'=>'状态','name'=>'xsstatustitle'),
			array('name'=>'open_time','title'=>'开班时间','type'=>'time'),
			array('name'=>'payfee','title'=>'单费','type'=>'string'),
			array('name'=>'paytime','title'=>'收款日期','type'=>'time'),
			array('name'=>'pay_type','title'=>'交费方式','type'=>'radio2','options'=>'pay_types'),
			array('name'=>'zhanghu','title'=>'到账账户','type'=>'string'),
			array('type'=>'radio2','title'=>'类型','name'=>'noadd','options'=>'noadd'),
			array('name'=>'fee1','title'=>'著作权','type'=>'unitstring','unit'=>'元'),
			array('name'=>'fee2','title'=>'学校','type'=>'unitstring','unit'=>'元'),
			array('name'=>'fee3','title'=>'招生门店','type'=>'unitstring','unit'=>'元'),
			array('type'=>'radio2','title'=>'状态','name'=>'status','options'=>'status'),
		),'action'=>array(
			array('type'=>'href','action'=>'jy_yxw/role14.fentan_log/check/','title'=>'分摊','show'=>array('status'=>0)),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','width'=>10,'title'=>'电话','type'=>'string'),
			array('name'=>'createtime','width'=>30,'title'=>'应缴时间','type'=>'time_range'),
			array('name'=>'storeid','width'=>15,'title'=>'所属门店','type'=>'select','options'=>'stores'),
		)
	);
	if($_RQ['status']==1){
		$htmls['list']['list'][]=array('name'=>'up_time','title'=>'实缴时间','type'=>'time');
		$htmls['list']['list'][]=array('name'=>'up_workerid','title'=>'操作人','type'=>'select','options'=>'workers');
	}
	$return['status']=array('待分摊','已分摊','分摊异常');
	$return['noadd']=array('进账','出账');
	$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['workers']=$this->getall('worker',array('role'=>14,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['class']=$this->getall('class',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	$return['pay_types']=$this->pay_types;
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if($post['fee1'] + $post['fee2'] + $post['fee3']>$post['payfee']){
			exi('【著作权】+【学校】+【招生门店】必须等于【单费】!','error');
		}
		$id=$post['id'];
		$data=array(
			'fee1'=>$post['fee1'],
			'fee2'=>$post['fee2'],
			'fee3'=>$post['fee3'],
			'zhanghu'=>$post['zhanghu'],
			'remark'=>trim($post['remark']),
			'status'=>intval($post['status']),
		);
		$data['up_time']=time();
		$data['up_workerid']=$_SESSION['workerid'];
		$this->update('fentan_log',$data);
		if($post['status']==1){
			pdo_update('jy_yxw_xuesheng',array('isfentan'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		}
		exi('操作成功！','','/role14.fentan_log/');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_fentan_log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$xuesheng=pdo_get('jy_yxw_xuesheng',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['xueshengid']));
		$item=array($xuesheng,$item);
		$item['storetitle']=pdo_getcolumn('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']),'title');
		$item['classtitle']=pdo_getcolumn('jy_yxw_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['classid']),'title');
		$item['classtitle']=$item['classtitle']?:'无';
		$item['statustitle']=$this->xuesheng_status[$xuesheng['status']];
		$infee_log=pdo_get('jy_yxw_infee_log',array('type'=>1,'memberid'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']));
		$item['paytime']=$infee_log['up_time'];
		$item['payfee']=$infee_log['fee'];
		$item['pay_type']=$infee_log['pay_type'];
		$item['open_time']=pdo_getcolumn('jy_yxw_class',array('id'=>$item['classid'],'uniacid'=>$_SESSION['uniacid']),'open_time');
		$item['open_time']=$item['open_time']?date('Y-m-d',$item['open_time']):'无';
		$item['paytime']=date('Y-m-d',$item['paytime']);
		$item['zhanghu']='公户';
		$item['pay_typetitle']=$this->pay_types[$item['pay_type']];
	}else{
		exi('请先选择记录','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
	);
	$htmls['edit']=array(
		array('name'=>'id','title'=>'ID','type'=>'copy'),
		array('name'=>'storetitle','title'=>'所属门店','type'=>'copy'),
		array('name'=>'title','title'=>'姓名','type'=>'copy'),
		array('name'=>'cardno','title'=>'身证号','type'=>'copy'),
		array('name'=>'mobile','title'=>'手机号','type'=>'copy'),
		array('name'=>'classtitle','title'=>'售后群','type'=>'copy'),
		array('name'=>'statustitle','title'=>'状态','type'=>'copy'),
		array('name'=>'open_time','title'=>'开班时间','type'=>'copy'),
		array('name'=>'payfee','title'=>'单费','type'=>'copy'),
		array('name'=>'paytime','title'=>'收款日期','type'=>'copy'),
		array('name'=>'pay_typetitle','title'=>'交费方式','type'=>'copy'),
		array('name'=>'zhanghu','title'=>'到账账户','type'=>'copy'),
		array('name'=>'fee1','title'=>'著作权','type'=>'unitstring','unit'=>'元'),
		array('name'=>'fee2','title'=>'学校','type'=>'unitstring','unit'=>'元'),
		array('name'=>'fee3','title'=>'招生门店','type'=>'unitstring','unit'=>'元'),
		array('type'=>'radio','title'=>'分摊状态','name'=>'status','options'=>'status'),
		array('type'=>'text','title'=>'分摊备注','name'=>'remark'),
	);
	$return['status']=array('待分摊','已分摊','分摊异常');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']==6){
		$params['status']=0;
		$tstart= mktime(0,0,0,date('m'),date('d'),date('Y'));
		$tend =$tstart+24*3600;
		$params['timenum between']=array($tstart,$tend);
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($_RQ['memberid']){
		$params['memberid']=intval($_RQ['memberid']);
		$return['params']['memberid']=$_RQ['memberid'];
	}
	if($_RQ['title']){
		$params2['title like']='%'.trim($_RQ['title']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['mobile']){
		$params2['mobile like']='%'.trim($_RQ['mobile']).'%';
		$memberids=pdo_getall('jy_yxw_member',$params2,'id','id');
		if($memberids){
			$memberids=array_keys($memberids);
		}else{
			$memberids=-1;
		}
		$params['memberid']=$memberids;
		$return['params']['mobile']=$_RQ['mobile'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['timenum between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('jy_yxw_fentan_log',$params,$fields);
	if($list){
		$return['status']=array('待分摊','已分摊','分摊异常');
		$return['noadd']=array('进账','出账');
		foreach($list as &$item){
			$item['member']=$this->model('xuesheng')->member_show($item['xueshengid']);
			$item['title']=$item['member']['nickname'];
			$xuesheng['status']=pdo_getcolumn('jy_yxw_class',array('id'=>$item['xueshengid'],'uniacid'=>$_SESSION['uniacid']),'status');
			$item['xsstatustitle']=$this->xuesheng_status[$xuesheng['status']];
			$infee_log=pdo_get('jy_yxw_infee_log',array('type'=>1,'memberid'=>$item['memberid'],'uniacid'=>$_SESSION['uniacid']));
			$item['paytime']=$infee_log['up_time'];
			$item['payfee']=$infee_log['fee'];
			$item['pay_type']=$this->pay_types[$infee_log['pay_type']];
			$item['status']=$return['status'][$item['status']];
			$item['noadd']=$return['noadd'][$item['noadd']];
			$item['classid']=pdo_getcolumn('jy_yxw_xuesheng',array('id'=>$item['xueshengid'],'uniacid'=>$_SESSION['uniacid']),'classid');
			$item['open_time']=pdo_getcolumn('jy_yxw_class',array('id'=>$item['classid'],'uniacid'=>$_SESSION['uniacid']),'open_time');
			$item['open_time']=date('Y-m-d',$item['open_time']);
			$item['paytime']=date('Y-m-d',$item['paytime']);
			$item['zhanghu']='公户';
			$item['fromstore']=pdo_getcolumn('jy_yxw_store',array('id'=>$item['storeid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['classid']=pdo_getcolumn('jy_yxw_class',array('id'=>$item['classid'],'uniacid'=>$_SESSION['uniacid']),'title');
		}
	}
	$header=array(
		'title'=>'售后姓名',
		'fromstore'=>'所属门店',
		'classid'=>'售后群',
		'open_time'=>'开班时间',
		'payfee'=>'单费',
		'paytime'=>'收款日期',
		'pay_type'=>'交费方式',
		'zhanghu'=>'到账账户',
		'noadd'=>'类型',
		'fee1'=>'著作权',
		'fee2'=>'学校',
		'fee3'=>'招生门店',
		'xsstatustitle'=>'状态',
	);
	model('core/tools')->export2excel($header,$list,'分摊数据');
}
